//
//  KRLCountryViewController.m
//  KaraLive
//
//  Created by Zhijia Zhong on 2018/9/13.
//  Copyright © 2018年 naxigoren. All rights reserved.
//

#import "KRLCountryViewController.h"
#import "KRLCountryModel.h"
#import "KRLCountrySearchTableViewController.h"

@interface KRLCountryViewController () <KRLCountrySearchDelegate>

@property (nonatomic, strong) NSArray *countrys;
@property (nonatomic, strong) NSString *cellReuseIdentifier;

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) KRLCountrySearchTableViewController *searchResultController;

@end

@implementation KRLCountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configUI];
    [self configData];
}

- (void)configUI {
    self.navigationItem.title = @"Choose Country";

    self.cellReuseIdentifier = @"KRLCountryCell";
    self.tableView.rowHeight = 40;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:self.cellReuseIdentifier];

    self.searchResultController = [[KRLCountrySearchTableViewController alloc] init];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultController];
    self.searchController.searchResultsUpdater = self.searchResultController;
    self.searchResultController.delegate = self;
    [self.searchController.searchBar sizeToFit];
    self.searchController.searchBar.placeholder = @"search";

    // iOS11之后searchController有了新样式，可以放在导航栏
    if (@available(iOS 11.0, *)) {
        self.navigationItem.searchController = self.searchController;
    } else {
        self.tableView.tableHeaderView = self.searchController.searchBar;
    }
    self.definesPresentationContext = YES;
    self.searchResultController.searchBar = self.searchController.searchBar;
}

- (void)configData {
    // 后台从plist文件加载国家信息
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Country" ofType:@"plist"];
        NSDictionary *datas = [NSDictionary dictionaryWithContentsOfFile:path];
        NSMutableArray *countrys = [NSMutableArray array];
        for (NSString *country in datas) {
            NSString *areaCode = datas[country][@"Area Code"];
            NSString *imagePath = datas[country][@"Icon"];
            KRLCountryModel *model = [[KRLCountryModel alloc] initWithName:country areaCode:areaCode imagePath:imagePath];
            [countrys addObject:model];
        }
        [countrys sortUsingComparator:^NSComparisonResult(KRLCountryModel *obj1, KRLCountryModel *obj2) {
            return [obj1.name compare:obj2.name];
        }];
        self.countrys = countrys;
        self.searchResultController.countrys = countrys;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.countrys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellReuseIdentifier forIndexPath:indexPath];
    
    KRLCountryModel *country = self.countrys[indexPath.row];
    cell.textLabel.text = country.name;
    cell.imageView.image = country.icon;
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_selectBlock) {
        KRLCountryModel *country = self.countrys[indexPath.row];
        _selectBlock(country.name, country.areaCode);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - KRLCountrySearchDelegate

- (void)didSelectCountry:(KRLCountryModel *)country {
    if (_selectBlock) {
        _selectBlock(country.name, country.areaCode);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
